import store from '@/store'
Array.prototype.insert = function (index, item) {
  this.splice(index, 0, item);
};
Array.prototype.remove = function(from, to) {
  var rest = this.slice((to || from) + 1 || this.length);
  this.length = from < 0 ? this.length + from : from;
  return this.push.apply(this, rest);
};

export const g_UserList = {
	members : {},
	private_chat_array:[],
	selected_uid : 0,
	menu_selected_uid : 0,
	admin_count : 0,
	moblie_count : 0,
	member_count : 0,
	
	GetUser : function(uid){
		if (uid in this.members){
			return this.members[uid];
		}
		return null;
	},
	
	GetSelectedUser : function(){
		if (this.selected_uid == 0){
			return null;
		}
		return this.members[this.selected_uid];
	},

	UserSelected : function(uid){
		this.selected_uid = uid;
	},
	UserIn : function(user){
		console.log('UserIn:'+user.appdata.nickname);
		//去重
		this.UserOut(user.uid);
		
		this.members[user.uid] = user;
		var before_u = null;
		var arrayLength = this.private_chat_array.length;
		if(((arrayLength<100 && user.rank>=111) || user.levelinroom>=500) && user.level !== 6000){
			for(var i=0;i<this.private_chat_array.length;i++){
				var u = this.private_chat_array[i];
				if(user.rank > u.rank || (user.rank == u.rank && user.appdata.nicegid < u.appdata.nicegid)){
					before_u = u;
					this.private_chat_array.insert(i,user);
					break;
				}
			}
			if(before_u === null){
				this.private_chat_array.push(user);
			}
		}
		store.commit("SET_PRIVATECHATARR",this.private_chat_array)
		if (user.level != 6000){
			this.member_count += 1;
		}
	},

	UserOut : function(uid){
		let user = g_UserList.GetUser(uid);
		if(!user){
			return
		}
		if (user.level != 6000 && this.member_count>0){
			this.member_count -= 1;
		}
		delete this.members[uid]
	},

	ChangeLevel : function(uid,level){
		if (this.members[uid] == null){
			return;
		}
		this.members[uid].level = level;
	},

	Clear : function(){
		for (var uid in this.members){
			this.UserOut(uid);
		}
		this.members = {};
		this.private_chat_array = [];
	}
};